package com.bosssoft.hr.javase;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetExample {


    public Student[] sort(Student[] students) {

         Set<Student> set = new TreeSet<Student>(new Comparator<Student>() {
            public int compare(Student o1, Student o2) {
//            return o1.getId().intValue() - o2.getId().intValue();
                return o1.getId() - o2.getId();
            }
        });

        for (int i = 0; i < students.length; i++) {
            set.add(students[i]);

        }


        return  set.toArray(new Student[] {});
    }

   public boolean add(Student student) {
       Set<Student> set=new TreeSet<Student>() ;

        return set.add(student);
    }

  public   boolean remove(Student student) {
      Set<Student> set=new TreeSet<Student>() ;
      set.add(new Student(1,"wang"));
      Student tmp=null;
        for (Student u: set) {
            if (u.getId() == student.getId() && u.getName().equals(student.getName())) {
                tmp = u;
                break;
            }
        }
        if (tmp == null) {
            return false;
        }
        return set.remove(tmp);
    }

   public boolean contains(Student student) {
       Set<Student> set=new TreeSet<Student>() ;
       set.add(new Student(1,"wang"));
        for (Student u: set) {
            if (u.getId() == student.getId() && u.getName().equals(student.getName())) {
                return true;
            }
        }
        return false;

    }

    public Student[] toArray() {
        Student[] students=new Student[2];
        students[0]=new Student(2,"张三");
        students[1]=new Student(1,"李四");
        return students;

    }

    public static void main(String[] args) {
        TreeSetExample n=new TreeSetExample();

        Student[] nn=new Student[2];
        nn[0]=new Student(2,"张三");
        nn[1]=new Student(1,"李四");
        Student[] dd=n.sort(nn);
        for (int i=0;i<dd.length;i++){
            System.out.println(dd[i]);
        }
    }


}
